package cn.tunyun.web.controller;


import cn.tunyun.common.JsonResult;
import cn.tunyun.domain.User;
import cn.tunyun.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class EmployeeController  {
    @Autowired
     private IUserService userService;
    @RequestMapping("/findall")
    //@ResponseBody
    public List<User> findAll(){
        List<User> userList = userService.findAll();
        System.out.println(userList+"................");
        return userList;
    }

    @RequestMapping("/findOne")
    @ResponseBody
    public User beforeEdit(Long id, String cmd){
        if(id!=null && "_update".equals(cmd)){
            //修改才执行这个代码
            User user = userService.findOne(id);
            //解决n-to-n的问题,把关联对象设置为null
            user.setNickname("手机用户"+user.getUsername());
            return  user;
        }
        return null;
    }

    @RequestMapping("/update")
    @ResponseBody
    public JsonResult update(@ModelAttribute("editEmployee")User employee){
        try {
            userService.save(employee);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }
    }
   /* @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            vipLoginLogService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }*/

}
